package com.plat.api.common.task;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.plat.api.dao.api.PlatWechatLiveRoomDao;
import com.plat.api.entity.api.PlatWechatLiveRoomEntity;
import com.plat.api.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: shish
 * Date: 2020/10/13
 * Time: 15:17
 * Description: 修改直播间状态
 */
@Slf4j
public class updateLiveRoomStatus implements Runnable {
    private static String updateLiveRoomStatusKEY = "updateLiveRoomStatusKEY";
    private PlatWechatLiveRoomDao platWechatLiveRoomDao;
    private WxMaService wxMaService;
    private RedisCache redisCache;
    private static Integer END = 103;
    private static Integer FINISH = 107;

    public updateLiveRoomStatus(PlatWechatLiveRoomDao platWechatLiveRoomDao, WxMaService wxMaService, RedisCache redisCache) {
        this.platWechatLiveRoomDao = platWechatLiveRoomDao;
        this.wxMaService = wxMaService;
        this.redisCache = redisCache;
    }

    private String url = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=";

    @Override
    public void run() {
        log.info("启动更新直播间状态任务");
        try {
            Integer start = 0;
            Integer pageSize = 100;
            while (true) {
                Thread.sleep(1000 * 10);
                JSONObject map = new JSONObject();
                map.put("start", start);
                map.put("limit", pageSize);
                String result = HttpUtil.post(url + wxMaService.getAccessToken(), map.toJSONString());
                JSONArray res = JSONObject.parseObject(result).getJSONArray("room_info");
//                log.info("[查询信息：{}]", res.toString());
                for (Object obj : res) {
                    JSONObject jsonObject = (JSONObject) obj;
                    PlatWechatLiveRoomEntity roomEntity = platWechatLiveRoomDao.createLambdaQuery().andEq("room_id", jsonObject.getLong("roomid")).single();
                    if (roomEntity != null) {
                        log.info("[遍历所有房间：{}]", res.toString());
                        Integer liveStatus = jsonObject.getInteger("live_status");
                        roomEntity.setLiveStatus(liveStatus);
                        if (roomEntity.getLiveStatus().equals(END) || roomEntity.getLiveStatus().equals(FINISH)) {
                            if (roomEntity.getLiveEndTime() == null) {
                                roomEntity.setLiveEndTime(new Date());
                            }
                        }
                        log.info("开始更新数据状态：{}",liveStatus);
                        platWechatLiveRoomDao.updateById(roomEntity);
                    }

                }
                if (res.size() > 0) {
                    start = (start+1)+pageSize+1;
                } else {
                    start = 0;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("[获取房间号码失败:{}]", e.getMessage());
        }

    }


}
